from array import array 
import random
import pickle
import marshal
import sys
titles = {}

def lower(s):
   return s

   
def sprawdz(name):
    f = open(name)
    answer = 0
    link = False
    for line in f:
        if line == '\n':
            if link:
               answer += 1
            link = False
        elif line[0] == ' ':
            link = True
    return answer
        


def initTitles(name):

    out = open('output.txt','w')
    f = open(name)
    number = 0
    for line in f:
        title = line.split('##')[0].strip()
        assert title not in titles
        titles[ title ] = number 
        number += 1
        """
        if line[0] != ' ' and  line[0] != '\n':
            if lower(line.strip()) not in titles:
                titles[ lower(line.strip()) ] = number
                out.write( lower(line.strip()) + ' ' + str(number) + '\n')
                number += 1
        elif line[0] == ' ':
            wierzcholki.add(number - 1)
        """
    out.close()
    
    return number


links = {}
links[0] = []


def initLinks(name):
    f = open(name)
    number = -1
    for line in f:
        if line[0] != ' ' and  line[0] != '\n':
            if line.strip() in titles:
                number = titles[  line.strip() ]
                if number not in links:
                    links[number] = []
        elif line == '\n':
            pass
        elif line[0] == ' ':
            if line.strip()  in titles:
                links[number].append( titles[ lower( line.strip() )] )
        else:
            assert(False)


allLinks = initTitles('titles.txt')
print 'init titles ok'


initLinks('wikilinki.txt')
print 'init links ok'

links[-1] = allLinks

f2 = open('titles','wb')
marshal.dump(titles,f2)
f2.close()
f1 = open('dict','wb')
marshal.dump(links,f1)
f1.close()
